package com.tengju.data.infrastructure.repository.convert;

import com.fasterxml.jackson.core.type.TypeReference;
import com.tengju.data.domain.model.user.StarLevelEnum;
import com.tengju.data.domain.model.user.UserIdCode;
import com.tengju.data.domain.model.wx.WxActivateStatus;
import com.tengju.data.domain.model.wx.WxUserInfo;
import com.tengju.data.domain.shared.IdObject;
import com.tengju.data.domain.shared.JsonUtil;
import com.tengju.data.infrastructure.persistence.mybatis.dataobject.WxUserInfoDO;

import java.util.Optional;

/**
 * @author: 李大仙
 * @date: 2021/11/5 11:12 上午
 */
public class WxUserInfoConvert {
    public static WxUserInfo convert(WxUserInfoDO wxUserInfoDO) {
        if (wxUserInfoDO == null) {
            return null;
        }
        return new WxUserInfo(
                UserIdCode.of(wxUserInfoDO.getIdCode()),
                wxUserInfoDO.getDepartmentId(),
                UserIdCode.of(wxUserInfoDO.getDominationIdCode()),
                UserIdCode.of(wxUserInfoDO.getHistoryDominationIdCode()),
                wxUserInfoDO.getWxActivateStatus() != 0 ? WxActivateStatus.form(wxUserInfoDO.getWxActivateStatus()) : null,
                StarLevelEnum.parseByStarLevel(wxUserInfoDO.getStarLevel()),
                JsonUtil.toList(wxUserInfoDO.getTags(), new TypeReference<>() {
                })
        );
    }

    public static WxUserInfoDO convert(WxUserInfo wxUserInfo) {
        if (wxUserInfo == null) {
            return null;
        }
        WxUserInfoDO wxUserInfoDO = new WxUserInfoDO();
        wxUserInfoDO.setIdCode(wxUserInfo.getIdCode().getValue());
        wxUserInfoDO.setDepartmentId(wxUserInfo.getDepartmentId());
        wxUserInfoDO.setDominationIdCode(Optional.ofNullable(wxUserInfo.getDominationIdCode()).map(IdObject::getValue).orElse(0L));
        wxUserInfoDO.setHistoryDominationIdCode(Optional.ofNullable(wxUserInfo.getHistoryDominationIdCode()).map(IdObject::getValue).orElse(0L));
        wxUserInfoDO.setWxActivateStatus(Optional.ofNullable(wxUserInfo.getWxActivateStatus()).map(WxActivateStatus::getCode).orElse(0));
        wxUserInfoDO.setStarLevel(wxUserInfo.getStarLevel().getCode());
        wxUserInfoDO.setTags(JsonUtil.toJSONString(wxUserInfo.getTagIdList()));
        return wxUserInfoDO;
    }
}
